package com.jjiang.bbmall.product.web;

import com.jjiang.bbmall.product.entity.CategoryEntity;
import com.jjiang.bbmall.product.service.CategoryService;
import com.jjiang.bbmall.product.vo.Catalog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        // 1.查出所有1级分类
        List<CategoryEntity> categoryEntityList = categoryService.getLevelCategories();

        // 有默认前缀：classpath:/templates/ 默认后缀：.html
        // 视图解析器进行拼串，得到结果
        model.addAttribute("categories", categoryEntityList);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatalogJson(){
        Map<String, List<Catalog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }
}
